package com.lzb.browser.activity;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Handler;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.browser.BaseAc;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.adapter.PictureAlbumAdapter;
import com.lzb.browser.basic.utils.RxUtil;
import com.lzb.browser.bean.AlbumBean;
import com.lzb.browser.bean.CollectBean;
import com.lzb.browser.databinding.ActivityDownloadPictureBinding;
import com.lzb.browser.utils.AppSpUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class DownloadPictureActivity extends BaseAc<ActivityDownloadPictureBinding> implements View.OnClickListener, OnItemClickListener {

    private PictureAlbumAdapter pictureAdapter;
    private List<AlbumBean> listShow = new ArrayList<>();
    private List<String> listSel = new ArrayList<>();
    private boolean isEdit;

    @Override
    protected int onCreate() {
        return R.layout.activity_download_picture;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDownloadPictureBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvEdit.setOnClickListener(this);
        mDataBinding.tvAllSel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
        mDataBinding.tvCompelete.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new PictureAlbumAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvEdit) {//编辑
            if (listShow.size() == 0) return;
            isEdit = true;
            pictureAdapter.setEdit(isEdit);
            mDataBinding.tvEdit.setVisibility(View.GONE);
            mDataBinding.llEdit.setVisibility(View.VISIBLE);
        } else if (id == R.id.tvAllSel) {//全选
            listSel.clear();
            for (AlbumBean albumBean : listShow) {
                albumBean.setSelected(true);
                listSel.add(albumBean.getPath());
            }
            pictureAdapter.notifyDataSetChanged();
        } else if (id == R.id.tvDelete) {//删除
            if (listSel.size() == 0) return;
            delete();
        } else if (id == R.id.tvCompelete) {//完成
            isEdit = false;
            pictureAdapter.setEdit(isEdit);
            mDataBinding.tvEdit.setVisibility(View.VISIBLE);
            mDataBinding.llEdit.setVisibility(View.GONE);
            listSel.clear();
            getData();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (pictureAdapter.getItem(position).isSelected()) {
                pictureAdapter.getItem(position).setSelected(false);
                listSel.remove(pictureAdapter.getItem(position).getPath());
            } else {
                pictureAdapter.getItem(position).setSelected(true);
                listSel.add(pictureAdapter.getItem(position).getPath());
            }
            pictureAdapter.notifyItemChanged(position);
        } else {
            gotoPreview(UriUtils.file2Uri(new File(pictureAdapter.getItem(position).getPath())));
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.downloadPicture;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new AlbumBean(file.getPath()));
            }
        }
        if (listShow.size() > 0) {
            pictureAdapter.setList(listShow);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.flPictureNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.flPictureNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {
                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (int i = 0; i < listSel.size(); i++) {
                            FileUtils.delete(listSel.get(i));
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        listSel.clear();
                        getData();
                    }
                });
            }
        }, 500);
    }

    //跳转到预览应用
    private void gotoPreview(Uri selUri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(selUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(intent, 0);
        if (resolveInfos != null && !resolveInfos.isEmpty()) {
            startActivity(intent);
        } else {
            ToastUtils.showShort(R.string.no_preview_app);
        }
    }

}